//
//  TSDatePickerViewController.swift
//  TopStock
//
//  Created by dyd on 2017/9/20.
//  Copyright © 2017年 dyd. All rights reserved.
//

import UIKit
import JavaScriptCore
protocol TSDatePickerViewControllerDelegate:NSObjectProtocol {
    func setDate(dateText:String?,jsFn:String?,modelContext:JSContext?)
}

class TSDatePickerViewController: UIViewController {

    weak var delegate: TSDatePickerViewControllerDelegate?
    @IBOutlet weak var backGroundView: UIView!
    @IBOutlet weak var sheetGroundView: UIView!
    
    @IBOutlet var heightConstraint: NSLayoutConstraint!
    @IBOutlet var widthConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var datePicker: UIDatePicker!
    var dateText:String? = nil
    var jsFn:String? = nil
    var modelContext:JSContext? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapAction = UITapGestureRecognizer.init(target: self, action: #selector(hideKeyBoardAction(_:)))
        self.view.addGestureRecognizer(tapAction)
        datePicker.addTarget(self, action: #selector(dateChanged(datePicker:)), for: .valueChanged)
        dateChanged(datePicker: datePicker)//初始化DateText
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        movePrinterOptionView(isHidden: false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        movePrinterOptionView(isHidden: true)
        super.viewWillDisappear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func hideKeyBoardAction(_ sender: UITapGestureRecognizer)
    {
        self.dismiss(animated: true, completion: nil)
//        delegate?.setDate(dateText: dateText)
    }
    
    
    func movePrinterOptionView(isHidden: Bool) {
        let positionY = (isHidden) ? view.frame.height : view.frame.height - sheetGroundView.frame.size.height - 30
        UIView.animate(withDuration: 0.1, animations: {
            self.sheetGroundView.frame = CGRect(x: self.sheetGroundView.frame.origin.x,
                                                y: positionY,
                                                width: self.sheetGroundView.frame.size.width,
                                                height: self.sheetGroundView.frame.size.height)
        }, completion: { _ in
            self.sheetGroundView.isHidden = isHidden
        })
    }
    
    func dateChanged(datePicker:UIDatePicker)
    {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date = datePicker.date
        let dateStr = formatter.string(from: date)
        dateText = stringToTimeStamp(stringTime: dateStr)
    }
    
    
    @IBAction func selectedAction(_ sender: UIButton)
    {
        delegate?.setDate(dateText: dateText, jsFn: jsFn, modelContext: modelContext)
        self.dismiss(animated: true, completion: nil)
    }
    
    func stringToTimeStamp(stringTime:String)->String {
        let datefmatter = DateFormatter()
        datefmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
        let date = datefmatter.date(from: stringTime)
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        let time = Int(dateStamp)
        let millisecond = CLongLong(round(Double(time*1000)))
        return String(millisecond)
    }
    
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
